package com.yonyougov.devops.cruxcisupport.role;

import com.yonyougov.devops.cruxcisupport.role.dto.*;
import com.yonyougov.devops.cruxcisupport.role.vo.LinkedRoleRespVo;
import com.yonyougov.devops.cruxcisupport.role.vo.NoLinkRoleVo;
import com.yonyougov.devops.cruxcisupport.role.vo.NotLinkedRoleByUserIdDto;
import com.yonyougov.devops.cruxcisupport.roleapp.RoleAppEntity;
import com.yonyougov.devops.cruxcisupport.roleapp.RoleAppService;
import com.yonyougov.devops.cruxcisupport.rolebutton.RoleButtonEntity;
import com.yonyougov.devops.cruxcisupport.rolebutton.RoleButtonService;
import com.yonyougov.devops.cruxcisupport.roleenv.RoleEnvEntity;
import com.yonyougov.devops.cruxcisupport.roleenv.RoleEnvService;
import com.yonyougov.devops.cruxcisupport.rolemenu.RoleMenuEntity;
import com.yonyougov.devops.cruxcisupport.rolemenu.RoleMenuService;
import com.yonyougov.devops.cruxcisupport.userrole.UserRoleEntity;
import com.yonyougov.devops.cruxcisupport.userrole.UserRoleService;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(value = "角色模块", tags = "角色模块")
@RestController
@RequestMapping("/api/v2/role")
public class RoleController extends CrudController<RoleEntity, BigInteger> {

    private final RoleService roleService;
    private final UserRoleService userRoleService;
    private final RoleEnvService roleEnvService;
    private final RoleAppService roleAppService;
    private final RoleMenuService roleMenuService;
    private final RoleButtonService roleButtonService;

    public RoleController(RoleService roleService, UserRoleService userRoleService, RoleEnvService roleEnvService, RoleAppService roleAppService, RoleMenuService roleMenuService, RoleButtonService roleButtonService) {
        this.roleService = roleService;
        this.userRoleService = userRoleService;
        this.roleEnvService = roleEnvService;
        this.roleAppService = roleAppService;
        this.roleMenuService = roleMenuService;
        this.roleButtonService = roleButtonService;
    }

    @Override
    public BaseService<RoleEntity, BigInteger> getService() {
        return roleService;
    }

    //<!--用户自定义代码开始-->

    @ApiOperation(value = "前端:新增角色", notes = "增加一个角色")
    @PostMapping(value = "/r/add")
    @SneakyThrows
    public BaseResponseDto addRole(@RequestBody @Valid RoleAddRequestDto request) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(request, roleEntity);
        roleEntity.setLocked(false);
        Date currentDate = new Date();
        roleEntity.setCreateTime(currentDate);
        roleEntity.setModifyTime(currentDate);
        roleService.add(roleEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端:删除角色 ", notes = "删除一个角色")
    @PostMapping(value = "/r/delete/id")
    @SneakyThrows
    public BaseResponseDto deleteRoleById(@RequestBody @Valid RoleDeleteRequestDto request) {
        RoleEntity roleEntity = new RoleEntity();
        roleEntity.setId(request.getId());
        roleEntity.setLocked(true);
        roleEntity.setModifyTime(new Date());
        BigInteger roleId = roleEntity.getId();
        // 删角色
        roleService.deleteByPrimaryKey(roleId);
        // 删 用户角色联系
        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setRoleId(roleId);
        userRoleService.delete(userRoleEntity);
        // 删 角色环境联系
        RoleEnvEntity roleEnvEntity = new RoleEnvEntity();
        roleEnvEntity.setRoleId(roleId);
        roleEnvService.delete(roleEnvEntity);
        // 删 角色应用联系
        RoleAppEntity roleAppEntity = new RoleAppEntity();
        roleAppEntity.setRoleId(roleId);
        roleAppService.delete(roleAppEntity);
        // 删 角色菜单联系
        RoleMenuEntity roleMenuEntity = new RoleMenuEntity();
        roleMenuEntity.setRoleId(roleId);
        roleMenuService.delete(roleMenuEntity);
        // 删 角色按钮联系
        RoleButtonEntity roleButtonEntity = new RoleButtonEntity();
        roleButtonEntity.setRoleId(roleId);
        roleButtonService.delete(roleButtonEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端:角色修改", notes = "修改一个角色的信息")
    @PostMapping(value = "/r/update")
    @SneakyThrows
    public BaseResponseDto updateRole(@RequestBody @Valid RoleUpdateRequestDto request) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(request, roleEntity);
        roleEntity.setModifyTime(new Date());
        roleService.update(roleEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "进入角色管理模块时渲染使用", notes = "可选择根据角色名称进行有效角色的模糊分页查询)")
    @SneakyThrows
    @PostMapping(value = "/r/page/like")
    public BaseResponseDto findRolePageLike(@RequestBody @Valid RolePageLikeRequestDto request) {
        RoleEntity roleEntity = new RoleEntity();
        BeanUtils.copyProperties(request, roleEntity);
        roleEntity.setLocked(false);
        Pagination<RoleEntity> roleEntityPagination = roleService.likeAllBy(roleEntity);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", roleEntityPagination.getTotalCount());
        result.put("data", roleEntityPagination.getDatas());
        return result;
    }

    @ApiOperation(value = "前端:用户关联角色,新增时的列表渲染", notes = "根据用户id查询未绑定的角色列表")
    @PostMapping(value = "/r/nolink/userid")
    public BaseResponseDto findNoLinkRoleByUserId(@RequestBody @Valid NotLinkedRoleByUserIdRequestDto request) {
        NotLinkedRoleByUserIdDto notLinkedRoleByUserIdDto = new NotLinkedRoleByUserIdDto();
        BeanUtils.copyProperties(request, notLinkedRoleByUserIdDto);

        Pagination<NoLinkRoleVo> datas = roleService.findNoLinkRoleByUserId(notLinkedRoleByUserIdDto);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", datas.getDatas());
        baseResponseDto.put("totalCount", datas.getTotalCount());
        return baseResponseDto;
    }

    @ApiOperation(value = "前端:获取已绑定的角色列表", notes = "根据用户id查询已绑定的角色列表")
    @PostMapping(value = "/r/linked/userid")
    public BaseResponseDto findLinkedRoleByUserId(@RequestBody @Valid LinkedRoleByUserIdRequestDto request) {
        List<LinkedRoleRespVo> dates = roleService.findLinkedRoleByUserId(request.getUserId());
        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("data", dates);
        return baseResponseDto;
    }

    //<!--用户自定义代码结束-->
}
